package com.dkd.findmyfriends;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ToggleButton;

public class SettingsActivity extends Activity implements OnItemSelectedListener, OnClickListener {

	private ToggleButton animToggle;
	
	@TargetApi(11)
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
		} else {
			requestWindowFeature(Window.FEATURE_NO_TITLE);
		}
        setContentView(R.layout.activity_settings);
        
        //grid size
        Spinner spinner = (Spinner) findViewById(R.id.gridSizeSpinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.grid_sizes, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);
        spinner.setSelection(GameSettings.getSettingsPosSelected());
        
        //animation toggle
        SharedPreferences sp = getSharedPreferences(SPKeys.PREFS_FILE_NAME, 0);
        boolean checked = sp.getBoolean(SPKeys.USE_FLIP_ANIMATION, false);
        animToggle = (ToggleButton) findViewById(R.id.anmtnTggl);
        animToggle.setChecked(checked);
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_settings, menu);
        return true;
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

	public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
		//reset selected images
		GameSettings.imagesSelected.clear();
		String size = String.valueOf(((TextView)view).getText());
		GameSettings.setGridSize(size);
		GameSettings.setSettingsPosSelected(pos);
		GameSettings.setNumCols(Integer.parseInt(size.split("x")[0]));
		GameSettings.setNumRows(Integer.parseInt(size.split("x")[1]));
	}

	public void onNothingSelected(AdapterView<?> parent) {
		
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.saveBtn:
			SharedPreferences.Editor e = getSharedPreferences(SPKeys.PREFS_FILE_NAME, 0).edit();
			e.putBoolean(SPKeys.USE_FLIP_ANIMATION, animToggle.isChecked());
			e.commit();
			finish();
		default:
			break;
		}
	}

}
